//
Boxy.DEFAULTS.title = '熊店';
Boxy.DEFAULTS.modal = true;
Boxy.DEFAULTS.closeText = '<b>x</b>';

var $bsSystemCode = {
    'success' : 'A00006'
};

function bsUrlencode($str)
{
    return encodeURIComponent($str);
}

function bsObjVal($obj , $k1 , $k2)
{
    if(typeof $obj != 'object')
        return null;
    else
    {
        if(typeof $obj[$k1] == 'object' && typeof $k2 !='undefined')
            return $obj[$k1][$k2];
        else
            return $obj[$k1];
    }
}

//处理接口返回数据
function bsFormatApiRs($string)
{
    return eval('('+$string+')');
}

//接口返回系统错误提示
function bsIsApiSystemError($code)
{
    
    $mCode2Alert = {
        "A00001":'系统繁忙'
    };
    
    if($code[0] == 'A' && $code != 'A00006')
    {
        
        if(eval("typeof $tl_lang.gCode2Alert."+$code) == 'undefined')
        {
            Boxy.alert('系统繁忙，请稍候重试!');
        }
        else
        {
            Boxy.alert(eval("$tl_lang.gCode2Alert."+$code));
        }
        return true;
    }
    return false;
}
//登录
function bsLogin( $att , $callback)
{
    if(typeof $callback != 'function')
    {
        $callback = function(){
            window.location.reload();
        };
    }
    //加入提示
    $map_attention = {
        'vote':{'q':'我为什么要登录？','a':'熊店致力给你最真实的信息，所以我们。。。。。'}
    };
    $html_login = '<div style="width:300px">\
邮箱：<input id="input_login_email" type="text"/><span id="alert_login_email"></span><br/>\
密码：<input id="input_login_passwd" type="password"/><span id="alert_login_pw"></span><br/>\
<a href="#" id="aLoginSubmit">登录</a> \
';
    if(eval('typeof $map_attention.'+$att) == 'object')
    {
        $oAttention = eval('$map_attention.'+$att);
        $html_login +=$oAttention.q+'<br/>'+$oAttention.a;
    }
    $html_login += '</div>'
    
    var $layer_login = new Boxy(
        $html_login,    //html
        {
            title:'登录',
            afterHide:$callback,
            behaviours:function(c)
            {
                c.find('#aLoginSubmit').click(function(){
                    //基本检查
                    $email = $('#input_login_email').val();
                    $passwd = $('#input_login_passwd').val();
                    if($email=='' || ! /\b(^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*$)\b/.test($email))
                    {
                        $('#alert_login_email').empty().append('错误');
                        return false;
                    }
                    else if($passwd == '' || !/^[0-9a-zA-z_=@#$%]{5,16}$/.test($passwd))
                    {
                        $('#alert_login_pw').empty().append('错误');
                        return false;
                    }
                    //清除错误提示
                    $('#alert_login_email , #alert_login_pw').empty();
                    //提交登录接口
                    $.post(
	                   '/api/login',
            	       'email='+$email+'&passwd='+$passwd,
            	       function($result)
            	       {
            	           $rs = bsFormatApiRs($result);
            	           //system error
            	           if(!bsIsApiSystemError($rs.code))
            	               return false;
            	               
            	           if($rs.code=='U10001')
            	           {
            	               $('#alert_login_email').empty().append('用户名或密码错误！');
            	               return false;
            	           }
            	           else if($rs.code=='U10002')
            	           {
            	               $('#alert_login_email').empty().append('帐号被封杀！');
            	               return false;
            	           }
            	           else if($rs.code=='A00006')
            	           {
            	               $scope.visitor.uid = $rs.data.uid;
            	               $scope.visitor.nick = $rs.data.nick;
            	               $('.close').click();
            	               return true;
            	           }
            	           return true;
            	       }
            	    );//post over
            	    
                });//login over
            }
        }
    );
}
//退出登录
function bsLogout()
{
    $.get(
        '/api/logout',
        '',
        function($result)
        {
            $rs = bsFormatApiRs($result);
            //system error
            if(!bsIsApiSystemError($rs.code))
                return false;
            
            window.location.reload();
        }
    );
}
//推荐标签
$bsTagLayer = {
    cb : null,
    init : function(cb){
        this.cb = cb;
        $str_html = '';
        $types = $scope.page_data.tag_type;
        for(i=0;i<$types.length;i++)
        {
            $str_html += '<span>'+$types[i]+'</span>';
            $values = $scope.page_data.tag_value[i];
            for(j=0;j<$values.length;j++)
            {
                $str_html += '<a onclick="$bsTagLayer.cb(\''+$values[j]+'\');" href="javascript:;">'+$values[j]+'</a>';
            }
        }
        $o = new Boxy(
            $str_html,
            {
                modal:false
            }
        );
    }
}
//alert
function bsCommonAlert($what , $alert)
{
    $('#alert_'+$what).empty().append($alert);
}
//basic check
function bsBasicCheck($what , $preg , $alert , $suc_alert)
{
    $value = $('#input_'+$what).val();
    
    if($value == '' || !$preg.test($value))
    {
        bsCommonAlert($what , $alert);
        $regi_check_pass=false;
        return false;
    }
    bsCommonAlert($what , $suc_alert);
    return true;
}

function triggerLeftMenu($id)
{
    $('#menuMap').hide();
    $('#menuMaterial').hide();
    $('#menuClass').hide();
    
    $('#'+$id).show();
}

function refresh_vcode()
{
    var $img_vcode = $("#img_vcode");
    $img_vcode.empty().append('<img src="/vcode?t='+Math.random()+'" />');  
}

$(document).ready(function(){
    
    //刷新验证码
    $('#aReVcode,#img_vcode').click(function(){
        $('#trVcode').show();
        setTimeout('refresh_vcode()',100);
        return false;
    });
    
    $('input').focus(function(){
        $id = $(this).attr('id');
        $id = "#alert_"+$id.replace('ipt' , '').toLowerCase();
        $($id).empty();
        
    });
    
    $('#btnSubmit').click(function(){
        $_username = $('#iptUsername').val();
        if($_username == "")
        {
            $('#alert_username').empty().append($tl_lang.pLoginAltUsernameNull);
            return false;
        }
        $_password = $('#iptPassword').val();
        if($_password == "")
        {
            $('#alert_password').empty().append($tl_lang.pLoginAltPasswordNull);
            return false;
        }
        
        //是否需要验证码
        $_vcode='';
        if($('#trVcode').css('display') != 'none')
        {
            $_vcode = $('#iptVcode').val();
            if($_vcode == "")
            {
                $('#alert_vcode').empty().append($tl_lang.pLoginAltVcodeNull);
                return false;
            }
        }
        $.post(
	       '/api/login',
	       'username=' + bsUrlencode($_username) 
	           + '&password=' + bsUrlencode($_password)
	           + '&vcode=' + bsUrlencode($_vcode),
	       function($result)
	       {
	           var $rs = bsFormatApiRs($result);
	           if(!bsIsApiSystemError($rs.code))
	           {
	               if($rs.code == $bsSystemCode.success)
	               {
	                   
    	               setTimeout(function(){
    	                 window.location='/navi';
    	               },3);
	               }
	               else
	               {
	                   if($scope.page_id=='p_login')
	                   {
    	                   $('#altLogin').empty().append(eval('$tl_lang.pLoginAltErr.'+$rs.code));
    	                   $('#aReVcode').click();
	                   }
	                   else
	                   {
	                       window.location='/login';
	                   }
	               }
	           }
	       }
	    );
	    return false;
    });
});

(function($) {
    $.fn.ttt = function(aPic,stoptime){
        var $dest = $(this);
        var i = 1;
        var status = 0
        var op = 0;
    	var timeout=30;
    	var imgAry = aPic
    	
        return this.each(function() {
	        
            setTimeout(run , stoptime);
	        function run()
        	{
            	op = $dest.css('opacity')*10;
            	if(status == 0)
            		op-=1;
            	else
            		op+=1;
            
            	$dest.css('opacity' , op/10);
            	if(op == 0)
            	{
            		$dest.attr("src", imgAry[i]);
            		i++;
            		if (i > imgAry.length - 1) {   
            			i = 0;   
            		}   
            		status=1;
            		timeout=30
            	}
            	else if(op == 10)
            	{
            		status=0
            		timeout=stoptime;
            	}
            	
            	setTimeout(run , timeout);
            	timeout=30;
        	}
        });
    }

})(jQuery);